package bTree.LeetCode;

public class 解码方法 {
    public int numDecodings(String s) {
        if (s.startsWith("0")) {
            return 0;
        }

        int length = s.length();
        int[] dp = new int[length + 1];
        dp[0] = 1;
        dp[1] = 1;
        for (int i = 1; i <= length; i++) {
            char c = s.charAt(i - 1);
            if (c == '0') {
                if (s.charAt(i - 2) >= '3' || s.charAt(i - 2) == '0') {
                    return 0;
                }
                dp[i] = dp[i - 2];
            } else {
                dp[i] = dp[i - 1];
                if (i != 1 && s.charAt(i - 2) != '0' && Integer.parseInt(s.substring(i - 2, i)) <= 26) {
                    dp[i] += dp[i - 2];
                }
            }
        }
        return dp[length];
    }

}